package com.pospi.pospihold.ui.pay_ui;

import android.util.Log;

import com.lany.sp.SPHelper;
import com.pospi.pospihold.App;
import com.pospi.pospihold.util.GetData;
import com.tsy.sdk.myokhttp.response.RawResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by acer on 2018/3/20.
 */

public class UnionConfig {
    //测试环境
//    public static final String URL_TOKEN = "http://58.247.0.18:29015/v1/token/access";
    public static final String URL_TERMINAL = "http://58.247.0.18:29015/v2/poslink/transaction/activeterminal";
//    public static final String URL_PAY = "http://58.247.0.18:29015/v2/poslink/transaction/pay";
//    public static final String URL_REFUND = "http://58.247.0.18:29015/v2/poslink/transaction/refund";
    public static final String URL_VOIDPAY = "http://58.247.0.18:29015/v2/poslink/transaction/voidpayment";
    public static final String TOKEN_MSG = "token_union";
    public static final String TOKEN_TIME = "token_time";
    public static final String APPID_CONFIG = "appid_config";
    public static final String APPKEY_CONFIG = "appkey_config";
    public static final String STORE_CONFIG = "store_config";
    public static final String MERCHANTCODE = "MERCHANTCODE";
    public static final String TERMINALCODE = "TERMINALCODE";
    public static final String ITEM_CONFIG = "item_config";
//    public static String APPID = "f0ec96ad2c3848b5b810e7aadf369e2f";
//    public static String APPKEY = "775481e2556e4564985f5439a5e6a277";
//    public static String merchantCode= SPHelper.getInstance().getString(STORE_CONFIG,"123456789900080");
//    public static String terminalCode= SPHelper.getInstance().getString(ITEM_CONFIG,"12345080");

    //正式环境
    public static String APPID = "469381425b10e378015b3dc360ed01dc";
    public static String APPKEY = "6e1b1475c3834a4a84e5936a2785f0c9";
    public static final String URL_TOKEN = "https://api-mop.chinaums.com/v1/token/access";
//    public static final String URL_TERMINAL = "https://103.95.220.37/v2/poslink/transaction/activeterminal";
    public static final String URL_PAY = "https://api-mop.chinaums.com/v2/poslink/transaction/pay";
    public static final String URL_REFUND = "https://api-mop.chinaums.com/v2/poslink/transaction/refund";
    //获取token
    public static void accessToken(){
        String tt = SPHelper.getInstance().getString(UnionConfig.TOKEN_TIME,"");

        if (!tt.equals("")&&!GetData.compareTime(GetData.getDataTime(), SPHelper.getInstance().getString(UnionConfig.TOKEN_TIME), "30")) {
            Log.i("pay", "token未失效:token="+ SPHelper.getInstance().getString(UnionConfig.TOKEN_MSG,""));
            return;
        }

        String time = GetData.getYYMMDDhhmmss();
        String nonce = GetData.getStringRandom(8);
        String signature = SHA1(UnionConfig.APPID + time + nonce + UnionConfig.APPKEY);
        JSONObject object = new JSONObject();
        try {
            object.put("appId", UnionConfig.APPID);
            object.put("timestamp", time);
            object.put("nonce", nonce);
            object.put("signature", signature);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.i("pay", object.toString());
//        map.put("Email", "test");
//        map.put("Password", "123456");
//        map.put("Imei", "000000000000");
        App.getInstance().getMyOkHttp().post()
                .url(UnionConfig.URL_TOKEN)
                .jsonParams(object.toString())
                .enqueue(new RawResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, String response) {
                        Log.i("pay", response);
                        try {
                            JSONObject json = new JSONObject(response);
                            SPHelper.getInstance().putString(UnionConfig.TOKEN_MSG,json.getString("accessToken"));
                            String time = GetData.getDataTime();
                            SPHelper.getInstance().putString(UnionConfig.TOKEN_TIME,time);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, String error_msg) {
                        Log.i("pay", error_msg);
                    }
                });
    }
    public static String SHA1(String decript) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

}
